<template>
  <el-dialog
    title='批量策略管理'
    :visible="policyvisible"
    @open="getPolicy"
    @close="close"
  >
    <el-form :model="form" :rules="rules" ref="policyForm">
      <el-form-item label="选择策略" label-width="120px" prop="policyId">
        <el-select
          v-model="form.policyId"
          placeholder="请选择"
          style="width: 80%"
        >
          <el-option
            v-for="item in strategyList"
            :key="item.policyId"
            :label="item.policyName"
            :value="item.policyId"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="onSubmit"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import Bus from '@/utils/bus'
import { getPolicy, putPolicy } from '@/api/device-manager'
export default {
  name: 'StrategyDialog',
  props: {
    policyvisible: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      form: {
        policyId: '',
        innerCodeList: [] // 选择的机器
      },
      rules: {
        policyId: [{ required: true, message: '请选择', trigger: 'blur' }]
      },
      strategyList: [] // 策略信息
    }
  },
  methods: {
    // 获取策略信息
    async getPolicy () {
      this.strategyList = await getPolicy()
    },
    // 取消
    close () {
      this.$refs.policyForm.resetFields()
      this.$emit('update:policyvisible', false)
    },
    // 批量操作
    onSubmit () {
      this.$refs.policyForm.validate(async value => {
        if (value) {
          await putPolicy(this.form)
          this.$message({ type: 'success', message: '操作成功' })
          this.close()
          this.$emit('updataList')
        }
      })
    }
  },
  mounted () {
    Bus.$on('getpolicy', data => {
      this.form.innerCodeList = data
    })
  }
}
</script>

<style>
</style>
